package com.szit.test.servlet;

import com.szit.test.biz.SaleBiz;
import com.szit.test.biz.impl.SaleBizImpl;
import com.szit.test.entity.PageIndexer;
import com.szit.test.entity.Sale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SearchTeacherServlet",value = "/sys/search")
public class SearchSaleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // StudentBiz sbiz = new StudentBizImpl();
        // List<Student> students = sbiz.list();

        request.setCharacterEncoding("utf-8");

        // 获取查询条件
        String sid = request.getParameter("id");
        String name = request.getParameter("name");
        String isreceived = request.getParameter("isreceived");
        System.out.println(isreceived);

        // 封装到对象中
        Sale searchTeacher = new Sale();
        if(sid != null && sid != ""){
            searchTeacher.setId(Integer.parseInt(sid));
        }

        searchTeacher.setName(name);

        // 获取分页页码
        String index = request.getParameter("pageIndex");
        // 定义分页器
        PageIndexer page = new PageIndexer(1,5,0);
        if(index != null){
            page.setPageIndex(Integer.parseInt(index));
        }



        // 调用逻辑层
        SaleBiz tbiz = new SaleBizImpl();
        List<Sale> teachers = tbiz.searchByPage(searchTeacher,isreceived,page);

        // 将集合保存到会话中
        request.getSession().setAttribute("teachers",teachers);

        // 保存查询条件的值
        request.getSession().setAttribute("search",searchTeacher);
        request.getSession().setAttribute("isreceived",isreceived);

        // 保存当前页
        request.getSession().setAttribute("page",page);

        // 跳转到列表页面
        response.sendRedirect("searchteacher2.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
